<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" @click="addRoles">添加角色</el-button>
      <div class="tablebox">
        <el-table :data="rolesData" border style="width: 100%">
          <el-table-column type="expand">
            <!-- 插槽 -->
            <template slot-scope="scope">
                         
              <el-row v-for="item1 in scope.row.children" :key="item1.id">
                             
                <!-- 渲染一级权限 -->
                             
                <el-col :span="5">
                                 
                  <el-tag
                    closable
                    @close="removeRightById(scope.row, item1.id)"
                    >{{ item1.authName }}</el-tag
                  >
                                  <i class="el-icon-caret-right"></i>          
                     
                </el-col>
                             
                <!-- 渲染二级和三级权限 -->
                             
                <el-col :span="19">
                                 
                  <el-row v-for="item2 in item1.children" :key="item2.id">
                                     
                    <el-col :span="6">
                                         
                      <el-tag
                        type="success"
                        closable
                        @close="removeRightById(scope.row, item2.id)"
                        >{{ item2.authName }}</el-tag
                      >
                                          <i class="el-icon-caret-right"></i>  
                                     
                    </el-col>
                                     
                    <el-col :span="18">

                      <el-tag
                        @close="removeRightById(scope.row, item3.id)"
                        type="warning"
                        v-for="item3 in item2.children"
                        :key="item3.id"
                        closable
                        >{{ item3.authName }}</el-tag
                      >
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"> </el-table-column>
          <el-table-column prop="roleName" label="角色名称"> </el-table-column>
          <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-setting"
                @click="fp(scope.row)"
                >分配权限</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <!-- 添加角色模态框 -->
    <el-dialog title="添加角色" :visible.sync="addRolesFormVisible">
      <el-form :model="addRoleForm" :rules="addRolesrules" ref="addRoleForm">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="roleName"
        >
          <el-input
            v-model="addRoleForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="roleDesc"
        >
          <el-input
            v-model="addRoleForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addRolesForm('addRoleForm')"
            >创建角色</el-button
          >
          <el-button @click="resetForm('addRoleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="编辑角色" :visible.sync="editRolesFormVisible">
      <el-form :model="addRoleForm" :rules="addRolesrules" ref="addRoleForm">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="roleName"
        >
          <el-input
            v-model="addRoleForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="roleDesc"
        >
          <el-input
            v-model="addRoleForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editRolesForm('addRoleForm')"
            >编辑角色</el-button
          >
          <el-button @click="resetForm('addRoleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisible2">
      <el-form>
        <el-tree
          :data="data"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree1"
          highlight-current
          :default-checked-keys="Keys"
          :props="defaultProps"
        >
        </el-tree>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="qqq()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRolesApi,
  addRolesApi,
  editRolesApi,
  deleteRolesApi,
  getRightsApi,
  TGRole,
} from "@/http/api";
import { Message } from "element-ui";
// import router from "@/router";
export default {
  name: "",
  components: {},
  data() {
    return {
      roleIds: 0,
      trees: [], // 所有权限
      defaultProps: {
        children: "children",
        label: "authName", // 标题信息
      },
      roleId: "", // 修改权限id
      Keys: [],
      rolesId: 0,
      rolesData: [],
      addRolesFormVisible: false,
      editRolesFormVisible: false,
      dialogFormVisible2: false,
      formLabelWidth: "120px",
      addRoleForm: {
        roleName: "",
        roleDesc: "",
      },
      addRolesrules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
    };
  },
  created() {
    this.getRoleData();
  },
  mounted() {},
  methods: {
    editRolesForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          editRolesApi(this.rolesId, {
            roleName: this.addRoleForm.roleName,
          }).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              Message({
                message: res.data.meta.msg,
                type: "success",
              });
            } else {
              Message({
                message: res.data.meta.msg,
                type: "error",
              });
            }
            this.getRoleData();
            this.editRolesFormVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addRolesForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addRolesApi(this.addRoleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 201) {
              Message({
                message: res.data.meta.msg,
                type: "success",
              });
            } else {
              Message({
                message: res.data.meta.msg,
                type: "error",
              });
            }
            this.getRoleData();
            this.addRolesFormVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getRoleData() {
      getRolesApi().then((res) => {
        console.log(res);
        this.rolesData = res.data.data;
        if (res.data.meta.status == 200) {
          Message({
            message: res.data.meta.msg,
            type: "success",
          });
        } else {
          Message({
            message: res.data.meta.msg,
            type: "error",
          });
        }
      });
    },
    addRoles() {
      this.addRolesFormVisible = true;
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.editRolesFormVisible = true;
      this.addRoleForm = row;
      this.rolesId = row.id;
    },
    handleDelete(index, row) {
      console.log(index, row);
      deleteRolesApi(row.id).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          Message({
            message: res.data.meta.msg,
            type: "success",
          });
        } else {
          Message({
            message: res.data.meta.msg,
            type: "error",
          });
        }
        this.getRoleData();
      });
    },
    // 分配管理
    fp(row) {
      this.roleIds=row.id
      // console.log(index, row);
      this.dialogFormVisible2 = true;
      getRightsApi("tree").then((res) => {
        console.log(res);
        this.data = res.data.data;
      });
      this.www(row, this.Keys);
    },
    www(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.www(item, arr));
    },
    async qqq() {
      let res;
      const key = [
        ...this.$refs.tree1.getCheckedKeys(),
        ...this.$refs.tree1.getHalfCheckedKeys(),
      ];
      const ById = key.join(",");
      console.log(ById);
      try {
        console.log(this.roleIds, ById);
        res = await TGRole(this.roleIds, ById );
      } catch (error) {
        // res.error
      }
      this.dialogFormVisible2 = false;
      this.getRoleData();
    },
  },
};
</script>
<style scoped lang="scss">
.box-card {
  margin-top: 20px;
  .tablebox {
    margin-top: 20px;
  }
}
</style>
